<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="通知类型" prop="noticeType">-->
<!--        <el-select v-model="queryParams.noticeType" placeholder="请选择通知类型">-->
<!--          <el-option-->
<!--            v-for="dict in noticeTypes"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="parseInt(dict.value)"-->
<!--          ></el-option>-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="排序" prop="sort">-->
<!--        <el-select v-model="queryParams.sort" placeholder="请选择">-->
<!--          <el-option-->
<!--            v-for="dict in sortType"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="parseInt(dict.value)"-->
<!--          ></el-option>-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="审核状态" prop="dr">-->
<!--        <el-select v-model="queryParams.dr" placeholder="请选择">-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.audit_status"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          ></el-option>-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['information:notice:add']"-->
<!--        >新增-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['information:notice:edit']"-->
<!--        >修改-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['information:notice:remove']"-->
<!--        >删除-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

    <el-table v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange" height="650" fixed>
<!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="消息Id" align="center" prop="id"/>
      <el-table-column label="通知类型" align="center" prop="noticeType">
        <template slot-scope="scope">
          <div v-for="item in noticeTypes">
            <span v-if="item.value==scope.row.noticeType">{{ item.label }}</span>
          </div>
          <!--          <dict-tag :options="noticeTypes" :value="scope.row.noticeType"/>-->
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="title"/>
      <el-table-column label="内容" align="center" prop="content">
        <template slot-scope="scope">
          {{ changeCompanyIntroduce(scope.row.content) }}
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="note"/>
      <el-table-column label="添加时间" align="center" prop="createTime"/>
      <el-table-column label="审核状态" align="center" prop="dr">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.audit_status" :value="scope.row.dr"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          ><span>查看</span>
          </el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['information:notice:remove']"-->
<!--          >删除-->
<!--          </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改系统通知对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="759px" :close-on-click-modal='false' append-to-body center>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="通知类型" prop="noticeType">
          <el-select disabled class="kind" v-model="form.noticeType" @change="changeNoticeType" placeholder="请选择通知类型">
            <el-option
              v-for="dict in noticeTypes"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="接收人" prop="sendUserId" v-if="form.noticeType === 1">
          <el-select disabled v-model="form.sendUserId" multiple filterable placeholder="请选择用户">
            <el-option
              v-for="dict in userList"
              :key="dict.userId"
              :label="dict.nickName"
              :value="dict.userId+''"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="接收角色" prop="sendUserId" v-if="form.noticeType === 2">
          <el-select disabled v-model="form.sendUserId" multiple  filterable placeholder="请选择角色">
            <el-option
              v-for="dict in dict.type.user_type"
              :key="dict.label"
              :label="dict.label"
              :value="dict.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input disabled class="kind" v-model="form.title" placeholder="请输入标题"/>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input disabled  class="kind" v-model="form.content" type="textarea"/>
        </el-form-item>
        <el-form-item label="备注" prop="note">
          <el-input disabled class="kind" v-model="form.note" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="审核备注" v-if="form.dr!=0" prop="remark">
          <el-input disabled class="kind"  type="textarea" v-model="form.remark"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 审核 -->
    <el-dialog title="审核" :visible.sync="buildingAudit" close-on-click-modal center width="750px">
      <el-form :model="form" label-width="130px">
        <el-form-item label="审核备注" prop="remark">
          <el-input class="kind" type="textarea" v-model="form.remark"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="success" v-if="form.dr===0" @click="submitCheck(3)">审核通过</el-button>
        <el-button type="success" v-if="form.dr===3" @click="submitCheck(1)">审核通过</el-button>
        <span style="width: 5%;display: inline-block"></span>
        <el-button type="danger" v-if="form.dr===0" @click="submitCheck(2)">审核驳回</el-button>
        <el-button type="danger" v-if="form.dr===3" @click="submitCheck(4)">审核驳回</el-button>
        <span style="width: 5%;display: inline-block"></span>
        <el-button type="info" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addNotice, delNotice, getNotice, listNotice, listNoticeX, updateNotice} from "@/api/information/notice";
import {listUserAll} from "@/api/information/user";
import MyEditor from "@/components/wangEdiot/index.vue";

export default {
  name: "Notice",
  components: {MyEditor},
  dicts: ['notice_type','audit_status','user_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      buildingAudit: false,
      // 总条数
      total: 0,
      // 系统通知表格数据
      noticeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        noticeType: null,
        sendUserId: null,
        title: null,
        content: null,
        note: null,
        sort: null,
        state: null,
        dr: null,
      },
      // 表单参数
      form: {},
      userList: [],
      noticeTypes: [
        {
          value: 0,
          label: "全部用户"
        },
        {
          value: 1,
          label: "指定用户"
        },
        {
          value: 2,
          label: "指定角色"
        },
      ],
      sortType: [
        {
          value: 0,
          label: "按时间倒序"
        },
        {
          value: 1,
          label: "按时间正序"
        },
      ],
      // 表单校验
      rules: {
        noticeType: [
          {required: true, message: "通知类型不能为空", trigger: "blur"}
        ],
        sendUserId: [
          {required: true, message: "接收人不能为空", trigger: "blur"}
        ],
        title: [
          {required: true, message: "标题不能为空", trigger: "blur"},
          {max: 100, message: '长度必须小于 100', trigger: 'blur'}
        ],
        content: [
          {required: true, message: "内容不能为空", trigger: "blur"},
          {max: 500, message: '长度必须小于 500', trigger: 'blur'}
        ],
        note: [
          {max: 100, message: '长度必须小于 100', trigger: 'blur'}
        ],
        remark: [
          {max: 100, message: '长度必须小于 100', trigger: 'blur'}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getUserList();
  },
  methods: {
    /** 转化富文本去除html格式*/
    changeCompanyIntroduce(richtext) {
      if (richtext != "" && richtext != null) {
        let len = 10
        let content = richtext.replace(/<.+?>/g, '');
        content = content.replace(/ /ig, ''); /* 去除  */
        content = content.replace(/\s/ig, ''); /* 去除空格 */
        if (content.length > len) {
          content = content.substring(0, len) + '...';
        }
        return content;
      }
      return "";
    },
    /** 提交审核 */
    submitCheck(state) {
      updateNotice({
        id: this.form.id,
        dr: state,
        remark: this.form.remark
      }).then(response => {
        this.$modal.msgSuccess("审核成功");
        this.buildingAudit = false;
        this.getList();
      });
    },
    /** 审核 */
    spaceAudit(row) {
      this.reset();
      this.form.id = row.id
      this.buildingAudit = true;
      this.form.remark = row.remark
      this.form.dr = row.dr
    },
    changeNoticeType() {
      this.form.sendUserId = null;
    },
    getUserList() {
      listUserAll().then(response => {
        this.userList = response.data
      })
    },
    /** 查询系统通知列表 */
    getList() {
      this.loading = true;
      listNoticeX(this.queryParams).then(response => {
        this.noticeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.buildingAudit = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        noticeType: null,
        sendUserId: null,
        title: null,
        content: null,
        note: null,
        sort: this.$store.getters.userId,
        state: null,
        dr: 0,
        createTime: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加系统通知";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getNotice(id).then(response => {
        this.form = response.data;
        if (this.form.sendUserId!=null){
          this.form.sendUserId = this.form.sendUserId.split(",")
        }
        this.open = true;
        if (this.$store.getters.userId !== this.form.sort) {
          this.title = "系统通知详情";
        } else {
          this.title = "修改系统通知";
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      if (this.form.sendUserId!=null){
        this.form.sendUserId = this.form.sendUserId.join(",")
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.form.dr = 0
            updateNotice(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNotice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除系统通知编号为"' + ids + '"的数据项？').then(function () {
        return delNotice(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('information/notice/export', {
        ...this.queryParams
      }, `notice_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.kind {
  width: 90%;
}
</style>
